import { loadSprite } from '../helper/Loader'
import SetStreak from '../helper/UI/effect/SetStreak';
const { ccclass, property } = cc._decorator;
interface DropItemProp {
  context: cc.Node // 添加节点的上限
  speed?: number // 下落速度/s
  createCallback?: Function // 创建成功回调
  finishCallback?: Function // 节点下落回调
  /**
   * 节点被杀死回调
   */
  killedCallback?: Function // 
}
export default class DropItem extends cc.Component {
  private taskNodePool: cc.NodePool = new cc.NodePool()
  private context: cc.Node
  private createCallback: Function
  private finishCallback: Function
  private killedCallback: Function

  constructor({ createCallback, killedCallback, finishCallback, context }: DropItemProp) {
    super()
    this.context = context
    this.createCallback = createCallback
    this.killedCallback = killedCallback
    this.finishCallback = finishCallback
  }
  /**
   * @description 创建下落节点
   * @author 刘建文
   * @date 2019-10-11
   * @private
   * @memberof DropItem
   */
  public async Create(speed = 1, { a, b, sum }: any) {
    const { width, height } = this.context
    cc.log('坐标', height)
    let node = null
    if (this.taskNodePool.size() > 0) {
      node = this.taskNodePool.get()
      cc.log('从缓存对象中创建的节点', node)
    } else {
      node = new cc.Node('dropNode')
      cc.log('新建节点', node)
    }
    node.removeAllChildren()
    // 背景节点
    const backNode = new cc.Node('background')
    node.addChild(backNode)
    // const sp = backNode.addComponent(cc.Sprite)
    // sp.spriteFrame = await loadSprite('image/pumpkin', cc.SpriteFrame)
    const labelNode = new cc.Node('label')
    labelNode.color = cc.color(255, 255, 255)
    backNode.addChild(labelNode)
    const label = labelNode.addComponent(cc.Label)
    label.string = `${a} x (?) = ${sum}`
    label.lineHeight = 40
    label._updateRenderData(true)

    // const parentNode = labelNode
    // const color = cc.color(255, 255, 255)
    // const resourceUrl = 'texture/singleColor'

    // const subNode = new cc.Node('streak')
    // parentNode.addChild(subNode)
    // const streak = subNode.addComponent(cc.MotionStreak)
    // subNode.width = labelNode.width
    // streak.color = color || cc.color(255, 0, 0)
    // streak.stroke = labelNode.width / 2
    // streak.fastMode = true
    // streak.fadeTime = 0.1
    // const streakRes = await loadSprite(resourceUrl, cc.Texture2D)
    // streak.texture = streakRes
    // const faburl = 'prefab/streak_fire_1'
    // let aaa = await loadSprite(faburl, cc.Prefab)
    // cc.log(aaa)
    // const fabNode = cc.instantiate(aaa)
    // cc.log(fabNode)
    // subNode.addChild(fabNode)


    // labelNode.position = cc.v2(0, -50)
    backNode.setContentSize(cc.size(labelNode.width, labelNode.height))
    // 节点添加碰撞检测组件
    const collider = node.addComponent(cc.BoxCollider)
    collider.name = `dropItemCollider_${new Date().getTime()}`
    collider.size = cc.size(backNode.width, backNode.height)
    collider.onCollisionEnter = (...args) => {
      cc.log('peng', args)
      this.finishCallback && this.finishCallback()
    }
    // 世界坐标顶部
    let x = Math.floor(Math.random() * (width - 200)) + 100
    const v2 = cc.v2()
    this.context.convertToNodeSpaceAR(cc.v2(x, height), v2)
    cc.log(height)
    node.setPosition(v2)

    this.context.addChild(node)
    // 创建下落节点并添加
    node.group = 'dropItem'
    // node.IsKinematic = false
    // 世界坐标底部
    const target = cc.v2()
    this.context.convertToNodeSpaceAR(cc.v2(x, 0 + node.height / 2), target)
    this.createCallback && this.createCallback(node)
    cc.tween(node).to(speed, { position: target }, { easing: 'linear' }).start()
    return node
  }
  public Kill(node: cc.Node) {
    // 停止所有动作，否则再次从对象池中取出会继续未完成动作
    node.stopAllActions()
    // 碰撞组件需要移除，避免一次碰撞触发了多次(只在新建节点时创建一次不生效))
    node.removeComponent(cc.BoxCollider)
    this.killedCallback && this.killedCallback(node)
    // 回收
    this.taskNodePool.put(node)
    cc.log(this.context)
  }
}
